package ch10.d1;

import org.junit.Test;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.stream.Stream;

public class FilesTest {

    @Test
    public void testList() throws IOException {
        Path p=Path.of("d:/filetest");
        // 列出目录下的所有子文件
        //流
        Stream<Path> s =Files.list(p);
        // 迭代器
        s.forEach( e-> System.out.println(e) );

    }

    @Test
    public void testNewDirectoryStream() throws IOException {
        Path p=Path.of("d:/filetest");
        DirectoryStream<Path> ds=Files.newDirectoryStream(p);
//        ds.forEach( e-> System.out.println(e) );

        for (Path e : ds) { //迭代器
            System.out.println(e);
        }
    }

    @Test
    public void testNewDirectoryStream2() throws IOException {
        Path p=Path.of("d:/filetest");
        //lambda表达式
        //treeSet 比较器  传参数，返回数字
        //forEach 消费者   传参数，没有返回值
        // newDirectoryStream 过滤器  传参数，返回boolean
        //Predicate 判定器

        // 所有的txt文件
//        DirectoryStream<Path> ds=Files.newDirectoryStream(p, e-> e.getFileName().toString().endsWith(".txt") );
        DirectoryStream<Path> ds=Files.newDirectoryStream(p, e-> Files.size(e)>1024*1024 );

        for (Path e : ds) { //迭代器
            System.out.println(e);
        }

    }

    @Test
    public void testNewDirectoryStream3() throws IOException {
        Path p=Path.of("d:/filetest");
        DirectoryStream<Path> ds=Files.newDirectoryStream(p,"*.txt");

        for (Path e : ds) { //迭代器
            System.out.println(e);
        }

    }

    @Test
    public void testWalk() throws IOException {
        Path p=Path.of("d:/filetest/png");
        Stream<Path> s=Files.walk(p);
        s.forEach(e-> System.out.println(e));
    }

    @Test
    public void testFind() throws IOException {
        Path p=Path.of("d:/filetest/png");
//        Stream<Path> s=Files.find(p,2, (t,u)-> t.getFileName().toString().endsWith(".png") );
//        Stream<Path> s=Files.find(p,3, (t,u)->!Files.isDirectory(t) );
//        Stream<Path> s=Files.find(p,Integer.MAX_VALUE, (t,u)->!Files.isDirectory(t) );
        Stream<Path> s=Files.find(p,Integer.MAX_VALUE, (t,u)->!u.isDirectory() );
        s.forEach(e-> System.out.println(e));
    }

    @Test
    public void testWalkFileTree() throws IOException {
        Path p=Path.of("d:/filetest/png");
        MyFileVistor fcv=new MyFileVistor();
        Files.walkFileTree(p,fcv );
        System.out.println(fcv.sum);
    }


    @Test
    public void testWalkFileTree2() throws IOException {
        Path p=Path.of("d:/filetest/png");


        final long[] sum = {0};
        // 匿名内部类：是FileVisitor接口的实现类
        Files.walkFileTree(p, new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("访问目录前："+dir);
                if(dir.getFileName().toString().equals("c")){
                    return FileVisitResult.SKIP_SUBTREE;
                }
                return FileVisitResult.CONTINUE;
            }
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                //使用堆内存
                sum[0] +=Files.size(file);


                System.out.println("访问文件："+file);
                if(file.getFileName().toString().equals("c1.jpg")){
//                    return FileVisitResult.TERMINATE;
                    return FileVisitResult.SKIP_SIBLINGS;
                }
                //
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                System.out.println("访问文件失败："+file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                System.out.println("访问目录后："+dir);
                return FileVisitResult.CONTINUE;
            }
        });

        System.out.println(sum[0]);
    }





}
